﻿@model NewsLetterSubscriptionListModel
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = T("Admin.Promotions.newsLetterSubscriptions").Text;
}
<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/Administration/Content/images/ico-promotions.png")" alt="" />
        @T("Admin.Promotions.NewsLetterSubscriptions")
    </div>
    <div class="options">
        <a href="@Url.Action("ExportCsv")" class="k-button">@T("Admin.Common.ExportToCsv")</a>
        <input type="submit" id="importcsv" name="importcsv" class="k-button" value="@T("Admin.Common.ImportFromCsv")" />
    </div>
</div>
<table width="100%">
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchEmail):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => model.SearchEmail)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.StoreId):
        </td>
        <td class="adminData">
            @Html.DropDownList("StoreId", Model.AvailableStores)
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" id="btnSearch" class="k-button" value="@T("Admin.Common.Search")" />
        </td>
    </tr>
</table>
<p>
</p>
<table class="adminContent">
    <tr>
        <td>
            <div id="newsletter-subscriptions-grid"></div>

            <script>
                $(document).ready(function() {
                    $("#newsletter-subscriptions-grid").kendoGrid({
                        dataSource: {
                            type: "json",
                            transport: {
                                read: {
                                    url: "@Html.Raw(Url.Action("SubscriptionList", "NewsLetterSubscription"))",
                                    type: "POST",
                                    dataType: "json",
                                    data: additionalData
                                },
                                update: {
                                    url: "@Html.Raw(Url.Action("SubscriptionUpdate", "NewsLetterSubscription"))",
                                    type: "POST",
                                    dataType: "json"
                                },
                                destroy: {
                                    url: "@Html.Raw(Url.Action("SubscriptionDelete", "NewsLetterSubscription"))",
                                    type: "POST",
                                    dataType: "json"
                                }
                            },
                            schema: {
                                data: "Data",
                                total: "Total",
                                errors: "Errors",
                                model: {
                                    id: "Id",
                                    fields: {
                                        Email: { editable: true, type: "string" },
                                        Active: { editable: true, type: "boolean" },
                                        StoreName: { editable: false, type: "string" },
                                        CreatedOn: { editable: false, type: "date" }
                                    }
                                }
                            },
                            requestEnd: function(e) {
                                if (e.type == "update") {
                                    this.read();
                                }
                            },
                            error: function(e) {
                                display_kendoui_grid_error(e);
                                // Cancel the changes
                                this.cancelChanges();
                            },
                            pageSize: @(defaultGridPageSize),
                            serverPaging: true,
                            serverFiltering: true,
                            serverSorting: true
                        },
                        pageable: {
                            refresh: true,
                            pageSizes: [@(gridPageSizes)]
                        },
                        editable: {
                            confirmation: false,
                            mode: "inline"
                        },
                        scrollable: false,
                        columns: [
                            {
                                field: "Email",
                                title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.Email")",
                                width: 300
                            }, {
                                field: "Active",
                                title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.Active")",
                                width: 100,
                                headerAttributes: { style: "text-align:center" },
                                attributes: { style: "text-align:center" },
                                template: '<img src="@Url.Content("~/Administration/Content/images/")active-#=Active#.gif" />'
                            }, {
                                field: "StoreName",
                                title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.Store")",
                                width: 300
                            },{
                                field: "CreatedOn",
                                title: "@T("Admin.Promotions.NewsLetterSubscriptions.Fields.CreatedOn")",
                                width: 200,
                                type: "date",
                                format: "{0:G}"
                            }, {
                                command: [
                                    {
                                        name: "edit",
                                        text: "@T("Admin.Common.Edit")"
                                    }, {
                                        name: "destroy",
                                        text: "@T("Admin.Common.Delete")"
                                    }
                                ],
                                width: 200
                            }
                        ]
                    });
                });
            </script>
        </td>
    </tr>
</table>
<script type="text/javascript">
    $(document).ready(function () {
        $('#btnSearch').click(function () {
            var grid = $('#newsletter-subscriptions-grid').data('kendoGrid');
            grid.dataSource.page(1); //new search. Set page size to 1
            grid.dataSource.read();
            return false;
        });

        $("#@Html.FieldIdFor(model => model.SearchEmail)").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#btnSearch").click();
                return false;
            }
        });
    });

    function additionalData() {
        return {
            StoreId: $('#@Html.FieldIdFor(model => model.StoreId)').val(),
            SearchEmail: $('#@Html.FieldIdFor(model => model.SearchEmail)').val()
        };
    }
</script>

@*import emails form*@
<div id="importcsv-window" style="display:none;">
    @using (Html.BeginForm("ImportCsv", "NewsLetterSubscription", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <table style="text-align:left;">
            <tr>
                <td>
                    @T("Admin.Common.CsvFile"):
                </td>
                <td>
                    <input type="file" id="importcsvfile" name="importcsvfile" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" class="k-button" value="@T("Admin.Common.ImportFromCsv")" />
                </td>
            </tr>
        </table>
    }
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $("#importcsv").click(function (e) {
            e.preventDefault();
            var window = $("#importcsv-window");
            if (!window.data("kendoWindow")) {
                window.kendoWindow({
                    modal: true,
                    width: "400px",
                    title: "@T("Admin.Common.ImportFromCsv")",
                    actions: ["Close"]
                });
            }
            window.data('kendoWindow').center().open();
        });
    });
</script>
